<template>
    <div class="">
        <Breadcrumb :route="routes" />
         <div class="wb-container">
            <div class="mian-content">
                <div class="search-content">
                    <div class="inp">
                        <el-input placeholder="请输入用户昵称" v-model="inputValue" @keydown.enter="vagueSearch"
                            style="width: 200px;"></el-input>
                        <el-button type="primary" @click="vagueSearch">搜索</el-button>
                        <el-button type="success" @click="getExcelMember">导出</el-button>
                    </div>
                </div>
                <div>
                    <el-table v-loading="loading" element-loading-text="加载数据..." :data="tableData"
                        style="width: 100%;height: 100%;" align="center"
                        :header-cell-style="{ background: '#44abf7', color: '#fff', fontSize: '14px', height: '40px', letterSpacing: '2px' }"
                        size="small">
                        <template #empty>
                            <el-empty description="暂无数据！" />
                        </template>
                        <el-table-column prop="nickname" label="昵称" align="center" />
                        <el-table-column prop="phone" label="电话" align="center" />
                        <el-table-column prop="head_img" label="头像" align="center">
                            <template #default="{ row }">
                                <div class="member-headImg">
                                    <el-image :src="row.head_img" class="memberImg" style="width: 50px;height: 50px;">
                                        <template #error>
                                            <div class="image-slot">
                                                <el-icon><icon-picture /></el-icon>
                                            </div>
                                        </template>
                                    </el-image>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="created_at" label="创建时间" align="center" />
                        <el-table-column label="操作" align="center" min-width="220" fixed="right">
                            <template #default="{ row }">
                                <el-button style="background-color: #D4D7DE;" @click="getMemberDetali(row)"
                                    v-has="'memberDetali'">详情</el-button>
                                <el-button type="primary" @click="getMemberOredr(row)"
                                    v-has="'memberOrder'">会员订单</el-button>
                                <el-button class="ProtectionBtnStyle" @click="ProtectionOredr(row)"
                                    v-has="'memberStatus'">维权</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="demo-pagination-block">
                    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"
                        :page-sizes="[15, 20, 25, 30]" :small="small" :disabled="disabled" :background="background"
                        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange" />
                </div>
            </div>
            <el-dialog v-model="dialogFormVisible" title="会员详情" width="30%">
                <el-form :model="data.form" :label-width="120" :label-position="labelPosition">
                    <el-form-item label="名称：">
                        <div>{{ data.form.nickname }}</div>
                    </el-form-item>
                    <el-form-item label="openid：">
                        <div>{{ data.form.openid }}</div>
                    </el-form-item>
                    <el-form-item label="头像：">
                        <img :src="data.form.head_img" alt="" style="width: 100px;height: 100px;">
                    </el-form-item>
                    <el-form-item label="创建时间：">
                        <div>{{ data.form.created_at }}</div>
                    </el-form-item>
                </el-form>
            </el-dialog>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, reactive, watch } from "vue";
import { useRoute } from "vue-router";
import Breadcrumb from '@/components/nav-header'
import { memberList, memberDetali } from '@/api/member'
import { ElPagination } from 'element-plus';
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { changeTableHead } from '@/utlis/index'
import * as xlsx from 'xlsx'
import { useRouter } from 'vue-router';

const route = useRoute()
const routes = ref()
const router = useRouter()
const tableData = ref()
const inputValue = ref('')
const loading = ref(true)
//获取会员详情
const labelPosition = ref("right")
const dialogFormVisible = ref(false)
const data = reactive({
    form: {
        nickname: '',
        openid: '',
        head_img: '',
        created_at: ''
    }
})
//分页的数据
const currentPage = ref(1)
const pageSize = ref(15)
const small = ref(true)
const background = ref(true)
const disabled = ref(false)
const total = ref(0)
const form = reactive({
    limit: 15,
    page: 1,
    keyword: '',
    orderBy: 'order_count'
})

onMounted(() => {
    routes.value = route.matched
    getmemberData()
})
const getmemberData = async () => {
    const memberData = await memberList(form).then(s => {
        const { code, data } = s
        loading.value = false
        if (code == 200) {
            tableData.value = data.data
            total.value = data.total
        }
    }).catch((err) => {
        loading.value = false
    })
}
const getMemberDetali = async (row) => {
    dialogFormVisible.value = true
    let memberId = row.id
    const detaliData = await memberDetali(memberId).then(result => {
        const { code, data } = result
        if (code == 200) {
            data.form = data
        }
    })
}
const vagueSearch = () => {
    form.keyword = inputValue.value
    getmemberData(form)
}
watch(inputValue, (newX) => {
    if (newX === '') {
        getmemberData()
    }
})
//获取会员的订单列表
const getMemberOredr = (item) => {
    let param = JSON.parse(JSON.stringify(item.id))
    router.push({
        name: 'orderList',
        state: { param }
    })
}
const ProtectionOredr = (it) => {
    let data = JSON.parse(JSON.stringify(it.id))
    router.push({
        name: 'status',
        state: { data }
    })
}
const handleSizeChange = (size) => {
    pageSize.value = size
    form.limit = pageSize.value
    getmemberData(form)
}
const handleCurrentChange = (current) => {
    currentPage.value = current
    form.page = currentPage.value
    getmemberData(form)
}
//导出EXCEL表格
const getExcelMember = () => {
    let firstName = {
        nickname: '昵称',
        openid: 'openid',
        created_at: '注册时间'
    }
    let goodsExcelData = changeTableHead(tableData.value, firstName)
    // // 创建工作表
    const data = xlsx.utils.json_to_sheet(goodsExcelData);
    // 创建工作簿
    const wb = xlsx.utils.book_new();
    data['!cols'] = [{ // 设置单元格宽度 
        wpx: 240
    }, {
        wpx: 100
    },
    {
        wpx: 200
    }
    ]
    let goodsListName = '会员列表'
    let fileName = 'Sheet2'
    // 将工作表放入工作簿中  
    xlsx.utils.book_append_sheet(wb, data, goodsListName);
    // 生成文件并下载
    xlsx.writeFile(wb, `${fileName}.xlsx`);
}

</script>

<style lang="less" scoped>
.search-content {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 20px;
}

.inp {
    display: flex;
    justify-content: flex-end;
    width: 500px;

    .el-input {
        margin-right: 10px;
    }
}

.ProtectionBtnStyle {
    background-color: #d4b106;
    color: #fff;
}

.demo-pagination-block {
    margin-top: 20px;
}

.member-headImg {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;

    .memberImg {
        width: 60px;
        height: 60px;
        background-color: #f0f0f0;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}

.demo-image__error .block {
    padding: 30px 0;
    text-align: center;
    border-right: solid 1px var(--el-border-color);
    display: inline-block;
    width: 49%;
    box-sizing: border-box;
    vertical-align: top;
}

.demo-image__error .demonstration {
    display: block;
    color: var(--el-text-color-secondary);
    font-size: 14px;
    margin-bottom: 20px;
}

.demo-image__error .el-image {
    padding: 0 5px;
    max-width: 300px;
    max-height: 200px;
    width: 100%;
    height: 200px;
}

.demo-image__error .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: var(--el-fill-color-light);
    color: var(--el-text-color-secondary);
    font-size: 40px;
}

.demo-image__error .image-slot .el-icon {
    font-size: 40px;
}

:deep(.el-dialog .el-dialog__body) {
    border-top: 1px solid #efefef;
}

:deep(.el-form-item .el-form-item__label) {
    font-size: 16px;
}

:deep(.el-button) {
    padding: 2px 6px;
    color: #fff;
}
</style>
